//
//  MyScoreViewController.m
//  1gene
//
//  Created by 1jiyin on 16/4/6.
//  Copyright © 2016年 1jiyin. All rights reserved.
//

#import "MyScoreViewController.h"

@interface MyScoreViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
/*
 *标题
 */
@property(nonatomic,copy)UILabel *myTitle;
/*
 *一元优惠券
 */
@property(nonatomic,copy)UILabel *numberLab1;
@property(nonatomic,assign)NSInteger number1;
@property(nonatomic,copy)UILabel *numberLab11;
/*
 *二元优惠券
 */
@property(nonatomic,copy)UILabel *numberLab2;
@property(nonatomic,assign)NSInteger number2;
@property(nonatomic,copy)UILabel *numberLab21;
/*
 *我的积分
 */
@property(nonatomic,assign)CGFloat myNumber;


@property(nonatomic,copy)UITextField *text1;
@property(nonatomic,copy)UITextField *text2;
@property(nonatomic,copy)UITextField *text3;
@property(nonatomic,copy)UITextField *text4;

@property(nonatomic,copy)UIScrollView *scrollView;

@property(nonatomic,copy)UITableView *myTableView;


@property(nonatomic,copy)NSArray *arr;


@property(nonatomic,copy)UITextField *myTextField;


@property(nonatomic,assign)NSInteger num1;//60

@property(nonatomic,assign)NSInteger num2;//150

@property(nonatomic,assign)NSInteger num3;//八折

@property(nonatomic,assign)NSInteger num4;//五折

@property(nonatomic,assign)NSInteger num5;//免费

@end

@implementation MyScoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    _myNumber=_score;
    [self createNavigation];
    [self initData];
    [self createUI2];
    [self getAllQuanData];
    // Do any additional setup after loading the view.
}

-(void)initData
{
    _arr=@[@{@"iamge":@"money_60",@"str1":[NSString stringWithFormat:@"60元代金券x%ld",_num1],@"str2":@"60"},@{@"iamge":@"money_150",@"str1":[NSString stringWithFormat:@"150元代金券x%ld",_num2],@"str2":@"150"},@{@"iamge":@"money_bazhe",@"str1":[NSString stringWithFormat:@"八折优惠券x%ld",_num3],@"str2":@"60"},@{@"iamge":@"money_wuzhe",@"str1":[NSString stringWithFormat:@"五折优惠券x%ld",_num4],@"str2":@"150"},@{@"iamge":@"money_free",@"str1":[NSString stringWithFormat:@"免费基因检测券x%ld",_num5],@"str2":@"300"}];
}

-(void)createNavigation
{
    self.navigationController.navigationBar.barTintColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    //返回
    UIButton *leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame=CGRectMake(0, 0, 25, 25);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"left_back"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    
    //标题
    UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
    title.text=[NSString stringWithFormat:@"%.1f分",_score];
    title.font=[UIFont systemFontOfSize:22];
    title.textAlignment=NSTextAlignmentCenter;
    title.textColor=[UIColor whiteColor];
    self.navigationItem.titleView=title;
    _myTitle=title;
    //积分说明
    UIButton *rightBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame=CGRectMake(0, 0, 50, 44);
    [rightBtn setTitle:@"积分说明" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    rightBtn.titleLabel.font=[UIFont systemFontOfSize:12];
    [rightBtn addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:rightBtn];
}
//积分说明
-(void)rightBtnClick:(UIButton *)sender
{
    ScoreExplainViewController *vc=[[ScoreExplainViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
//返回上一页
-(void)leftBtnClick:(UIButton *)sender
{
    self.tabBarController.tabBar.hidden=NO;
    self.navigationController.navigationBar.hidden=YES;
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)createUI2
{
    UITableView *table=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    table.delegate=self;
    table.dataSource=self;
    table.separatorStyle= UITableViewCellSeparatorStyleNone;
    table.showsVerticalScrollIndicator=NO;
    [self.view addSubview:table];
    _myTableView=table;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    for (id subView in cell.contentView.subviews) {
        [subView removeFromSuperview];
    }
    
    UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(10, 20, 100, 60)];
    imageView.image=[UIImage imageNamed:_arr[indexPath.row][@"iamge"]];
    [cell.contentView addSubview:imageView];
    
    UILabel *lab1=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame)+10, imageView.frame.origin.y+(60-20)/2.0, 0, 20)];
    lab1.text=_arr[indexPath.row][@"str1"];
    lab1.font=[UIFont systemFontOfSize:15];
    lab1.textColor=[UIColor blackColor];
    lab1.frame=CGRectMake(CGRectGetMaxX(imageView.frame)+10, lab1.frame.origin.y, [lab1 calculateWidthWithLable:lab1], 20);
    [cell.contentView addSubview:lab1];
    
//    UILabel *lab2=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame)+10, CGRectGetMaxY(lab1.frame), 0, 25)];
//    lab2.text=_arr[indexPath.row][@"str2"];
//    lab2.font=[UIFont systemFontOfSize:20];
//    lab2.textColor=[UIColor colorWithRed:226/255.0 green:79/255.0 blue:20/255.0 alpha:1];
//    lab2.frame=CGRectMake(CGRectGetMaxX(imageView.frame)+10, CGRectGetMaxY(lab1.frame), [lab2 calculateWidthWithLable:lab2], 25);
//    [cell.contentView addSubview:lab2];
//    
//    UILabel *lab3=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(lab2.frame), CGRectGetMaxY(lab1.frame), 0, 25)];
//    lab3.text=@"积分兑换";
//    lab3.font=[UIFont systemFontOfSize:15];
//    lab3.textColor=[UIColor grayColor];
//    lab3.frame=CGRectMake(CGRectGetMaxX(lab2.frame), CGRectGetMaxY(lab1.frame), [lab3 calculateWidthWithLable:lab3], 25);
//    [cell.contentView addSubview:lab3];
//    
//    NSArray *arr=@[@"兑换",@"赠人",@"索要"];
//    for (int i=0; i<arr.count; i++) {
//        UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
//        btn.frame=CGRectMake(CGRectGetMaxX(imageView.frame)+10+(45+5)*i, CGRectGetMaxY(lab2.frame)+5, 45, 20);
//        btn.titleLabel.font=[UIFont systemFontOfSize:15];
//        [btn setTitle:arr[i] forState:UIControlStateNormal];
//        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
//        btn.tag=10+i+indexPath.row;
//        btn.layer.borderColor=[UIColor grayColor].CGColor;
//        btn.layer.borderWidth=1.0;
//        btn.layer.cornerRadius=2.0;
//        
//        if (i==0) {
//            [btn addTarget:self action:@selector(duihuanBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//        }else if (i==1){
//            [btn addTarget:self action:@selector(zengrenBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//        }else if (i==2){
//            [btn addTarget:self action:@selector(suoyaoBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//        }
//        [cell.contentView addSubview:btn];
//    }
//    
//    
//    
//    
//    
//    UIImageView *addImageView=[[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame)+10+(45+5)*arr.count+10, imageView.frame.origin.y, 25, 25)];
//    addImageView.image=[UIImage imageNamed:@"score_add_normal"];
//    addImageView.tag=indexPath.row+300;
//    [cell.contentView addSubview:addImageView];
//    UIButton *addBtn=[UIButton buttonWithType:UIButtonTypeCustom];
//    addBtn.frame=addImageView.frame;
//    addBtn.tag=indexPath.row+3000;
//    addBtn.userInteractionEnabled=NO;
//    [addBtn addTarget:self action:@selector(addNumberBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//    [cell.contentView addSubview:addBtn];
//    
//    
//    
//    UILabel *lab4=[[UILabel alloc]initWithFrame:CGRectMake(addImageView.frame.origin.x, CGRectGetMaxY(addImageView.frame), 0, 25)];
//    lab4.text=@"1张";
//    lab4.font=[UIFont systemFontOfSize:14];
//    lab4.textColor=[UIColor grayColor];
//    lab4.tag=indexPath.row+6000;
//    lab4.frame=CGRectMake(addImageView.frame.origin.x, CGRectGetMaxY(addImageView.frame), [lab4 calculateWidthWithLable:lab4], 25);
//    [cell.contentView addSubview:lab4];
//
//    UIImageView *deleteImageView=[[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame)+10+(45+5)*arr.count+10, CGRectGetMaxY(lab4.frame), 25, 25)];
//    deleteImageView.image=[UIImage imageNamed:@"score_delete_normal"];
//    deleteImageView.tag=indexPath.row+400;
//    [cell.contentView addSubview:deleteImageView];
//    UIButton *deleteBtn=[UIButton buttonWithType:UIButtonTypeCustom];
//    deleteBtn.frame=deleteImageView.frame;
//    deleteBtn.tag=indexPath.row+4000;
//    [cell.contentView addSubview:deleteBtn];
//    deleteBtn.userInteractionEnabled=NO;
//    [deleteBtn addTarget:self action:@selector(deleteNumberBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//    
//    
//    
//    
//    
//    UIButton *sureBtn=[UIButton buttonWithType:UIButtonTypeCustom];                                                                                                                                                                                                               
//    sureBtn.frame=CGRectMake(CGRectGetMaxX(lab4.frame)+15, lab4.frame.origin.y+2, 45, 25);
//    sureBtn.titleLabel.font=[UIFont systemFontOfSize:15];
//    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
//    [sureBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
//    sureBtn.tag=indexPath.row+100;
//    sureBtn.layer.borderColor=[UIColor grayColor].CGColor;
//    sureBtn.layer.borderWidth=1.0;
//    sureBtn.layer.cornerRadius=2.0;
//    sureBtn.userInteractionEnabled=NO;
//    [cell.contentView addSubview:sureBtn];
//    
//    
//    UITextField *text=[[UITextField alloc]initWithFrame:CGRectMake(lab1.frame.origin.x, CGRectGetMaxY(lab2.frame)+5+20+15, CGRectGetMaxX(addImageView.frame)-lab1.frame.origin.x, 25)];
//    text.placeholder=@"请输入账号";
//    text.layer.borderColor=[UIColor grayColor].CGColor;
//    text.layer.borderWidth=1.0;
//    text.layer.cornerRadius=2.0;
//    text.tag=70+indexPath.row;
//    text.font=[UIFont systemFontOfSize:14];
//    UIView *textView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 30, 25)];
//    textView.tag=50+indexPath.row;
//    textView.backgroundColor=[UIColor grayColor];
//    text.rightView=textView;
//    text.rightViewMode=UITextFieldViewModeAlways;
//    UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
//    btn.frame=textView.bounds;
//    btn.tag=indexPath.row+200;
//    btn.userInteractionEnabled=NO;
//    [btn setTitle:@"确定" forState:UIControlStateNormal];
//    btn.titleLabel.font=[UIFont systemFontOfSize:14];
//    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [textView addSubview:btn];
//    [cell.contentView addSubview:text];
//    _myTextField=text;
//    text.userInteractionEnabled=NO;
    
    UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0, 99.0, self.view.frame.size.width, 1)];
    line.backgroundColor=[UIColor grayColor];
    [cell.contentView addSubview:line];
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100.0;
}

//兑换
-(void)duihuanBtnClick:(UIButton *)sender
{
    NSLog(@"=======%ld",sender.tag);
    NSLog(@"==========1");
    UIButton *btn=(id)[self.navigationController.view viewWithTag:sender.tag-10-0+100];
    btn.userInteractionEnabled=YES;
    btn.layer.borderColor=[UIColor colorWithRed:226/255.0 green:79/255.0 blue:20/255.0 alpha:1].CGColor;
    btn.backgroundColor=[UIColor colorWithRed:226/255.0 green:79/255.0 blue:20/255.0 alpha:1];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    UIImageView *imgView1=(id)[self.navigationController.view viewWithTag:sender.tag-10+300];
    UIImageView *imgView2=(id)[self.navigationController.view viewWithTag:sender.tag-10+400];
    imgView1.image=[UIImage imageNamed:@"score_add_selected"];
    imgView2.image=[UIImage imageNamed:@"score_delete_selected"];
    
    UIButton *addBtn=[self.navigationController.view viewWithTag:sender.tag-10-0+3000];
    addBtn.userInteractionEnabled=YES;
    
    UIButton *deleteBtn=[self.navigationController.view viewWithTag:sender.tag-10-0+4000];
    deleteBtn.userInteractionEnabled=YES;
}
//赠人
-(void)zengrenBtnClick:(UIButton *)sender
{
     NSLog(@"==========2");
    UIButton *btn=(id)[self.navigationController.view viewWithTag:sender.tag-10-1+200];
    btn.userInteractionEnabled=YES;
    btn.backgroundColor=[UIColor colorWithRed:226/255.0 green:79/255.0 blue:20/255.0 alpha:1];
    btn.layer.borderColor=[UIColor colorWithRed:226/255.0 green:79/255.0 blue:20/255.0 alpha:1].CGColor;
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    UITextField *text=(id)[self.navigationController.view viewWithTag:sender.tag-10-1+70];
    UIView *view=(id)[self.navigationController.view viewWithTag:sender.tag-10-1+50];
    text.rightView=view;
    text.userInteractionEnabled=YES;
}
//索要
-(void)suoyaoBtnClick:(UIButton *)sender
{
     NSLog(@"==========3");
    UIButton *btn=(id)[self.navigationController.view viewWithTag:sender.tag-10-2+200];
    btn.userInteractionEnabled=YES;
    btn.backgroundColor=[UIColor colorWithRed:226/255.0 green:79/255.0 blue:20/255.0 alpha:1];
    btn.layer.borderColor=[UIColor colorWithRed:226/255.0 green:79/255.0 blue:20/255.0 alpha:1].CGColor;
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    UITextField *text=(id)[self.navigationController.view viewWithTag:sender.tag-10-2+70];
    UIView *view=(id)[self.navigationController.view viewWithTag:sender.tag-10-2+50];
    text.rightView=view;
    text.userInteractionEnabled=YES;
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.navigationController.view endEditing:YES];
}

-(void)addNumberBtnClick:(UIButton *)sender
{
    UILabel *lab=(id)[self.navigationController.view viewWithTag:sender.tag-3000+6000];
    NSInteger number=[[lab.text substringWithRange:NSMakeRange(0, lab.text.length-1)]integerValue];
    NSLog(@"========%ld",number);
    if (number>20) {
        
    }else{
        number+=1;
        lab.text=[NSString stringWithFormat:@"%ld张",number];
        lab.frame=CGRectMake(lab.frame.origin.x, lab.frame.origin.y, [lab calculateWidthWithLable:lab], lab.frame.size.height);
    }
    
}
-(void)deleteNumberBtnClick:(UIButton *)sender
{
    UILabel *lab=(id)[self.navigationController.view viewWithTag:sender.tag-4000+6000];
    NSInteger number=[[lab.text substringWithRange:NSMakeRange(0, lab.text.length-1)]integerValue];
    NSLog(@"========%ld",number);
    if (number>=2) {
        number-=1;
        lab.text=[NSString stringWithFormat:@"%ld张",number];
        lab.frame=CGRectMake(lab.frame.origin.x, lab.frame.origin.y, [lab calculateWidthWithLable:lab], lab.frame.size.height);
    }else{
        
    }
    
}
























-(void)createUI
{
    NSArray *arr=@[@{@"name1":@"60元代金券x",@"name2":@"(60积分兑换)"},@{@"name1":@"150元代金券x",@"name2":@"(150积分兑换)"},@{@"name1":@"八折优惠券x",@"name2":@"(60积分兑换)"},@{@"name1":@"五折优惠券x",@"name2":@"(150积分兑换)"},@{@"name1":@"免费基因检测x",@"name2":@"(300积分兑换)"}];
    _scrollView=[[UIScrollView alloc]initWithFrame:self.view.bounds];
    _scrollView.showsVerticalScrollIndicator=NO;
    [self.view addSubview:_scrollView];
    CGFloat btn_w=50;
    CGFloat btn_h=20;
    for (int i=0; i<arr.count; i++) {
        //1元优惠券
        UILabel *lab1=[[UILabel alloc]initWithFrame:CGRectMake(10, 20+(btn_h+30+3+10)*i, 100, 30)];
        //lab1.backgroundColor=[UIColor yellowColor];
        lab1.text=[NSString stringWithFormat:@"%@2张",arr[i][@"name1"]];
        lab1.font=[UIFont systemFontOfSize:16];
        lab1.textColor=[UIColor redColor];
        lab1.frame=CGRectMake(10, 20+(btn_h+30+3+10)*i, [self calculateWidthWithLable:lab1], 30);
        [_scrollView addSubview:lab1];
        //说明
        UILabel *lab11=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(lab1.frame), lab1.frame.origin.y, 10, lab1.frame.size.height)];
        lab11.text=arr[i][@"name2"];
        lab11.font=[UIFont systemFontOfSize:12];
        lab11.textColor=[UIColor grayColor];
        //lab11.backgroundColor=[UIColor yellowColor];
        lab11.frame=CGRectMake(CGRectGetMaxX(lab1.frame), lab1.frame.origin.y, [self calculateWidthWithLable:lab11], lab1.frame.size.height);
        [_scrollView addSubview:lab11];
        
        //兑换
        UIButton *btn1=[UIButton buttonWithType:UIButtonTypeCustom];
        btn1.frame=CGRectMake(lab1.frame.origin.x, CGRectGetMaxY(lab1.frame)+3, btn_w, btn_h);
        btn1.tag=i*10+1;
        btn1.backgroundColor=[UIColor redColor];
        btn1.layer.borderColor=[UIColor redColor].CGColor;
        btn1.layer.borderWidth=1.0;
        btn1.layer.cornerRadius=3.0;
        btn1.titleLabel.font=[UIFont systemFontOfSize:14];
        [btn1 setTitle:@"兑换" forState:UIControlStateNormal];
        [btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btn1 addTarget:self action:@selector(btn1Click:) forControlEvents:UIControlEventTouchUpInside];
        [_scrollView addSubview:btn1];
        
        //赠人
        UIButton *btn2=[UIButton buttonWithType:UIButtonTypeCustom];
        btn2.frame=CGRectMake(CGRectGetMaxX(btn1.frame)+5, CGRectGetMaxY(lab1.frame)+3, btn_w, btn_h);
        btn2.tag=i*10+2;
        btn2.backgroundColor=[UIColor redColor];
        btn2.layer.borderColor=[UIColor redColor].CGColor;
        btn2.layer.borderWidth=1.0;
        btn2.layer.cornerRadius=3.0;
        btn2.titleLabel.font=[UIFont systemFontOfSize:14];
        [btn2 setTitle:@"赠人" forState:UIControlStateNormal];
        [btn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btn2 addTarget:self action:@selector(btn2Click:) forControlEvents:UIControlEventTouchUpInside];
        [_scrollView addSubview:btn2];
        
        //索要
        UIButton *btn3=[UIButton buttonWithType:UIButtonTypeCustom];
        btn3.tag=i*10+3;
        btn3.frame=CGRectMake(CGRectGetMaxX(btn2.frame)+5, CGRectGetMaxY(lab1.frame)+3, btn_w, btn_h);
        btn3.backgroundColor=[UIColor redColor];
        btn3.layer.borderColor=[UIColor redColor].CGColor;
        btn3.layer.borderWidth=1.0;
        btn3.layer.cornerRadius=3.0;
        btn3.titleLabel.font=[UIFont systemFontOfSize:14];
        [btn3 setTitle:@"索要" forState:UIControlStateNormal];
        [btn3 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btn3 addTarget:self action:@selector(btn3Click:) forControlEvents:UIControlEventTouchUpInside];
        [_scrollView addSubview:btn3];
        
        
        UIView *v1=[[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(btn3.frame)+5, btn3.frame.origin.y, self.view.frame.size.width-(CGRectGetMaxX(btn3.frame)+10), btn_h)];
        v1.tag=i*10+100;
        [_scrollView addSubview:v1];
        UIButton *deleteBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        deleteBtn.tag=i+300;
        deleteBtn.frame=CGRectMake(0, 0, 30, btn_h);
        deleteBtn.titleLabel.font=[UIFont systemFontOfSize:14];
        deleteBtn .backgroundColor=[UIColor orangeColor];
        [deleteBtn setTitle:@"-" forState:UIControlStateNormal];
        [deleteBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [deleteBtn addTarget:self action:@selector(deleteBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [v1 addSubview:deleteBtn];
        
        UIButton *addBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        addBtn.tag=i+400;
        addBtn.titleLabel.font=[UIFont systemFontOfSize:14];
        addBtn.frame=CGRectMake(CGRectGetMaxX(deleteBtn.frame)+5, 0, 30, btn_h);
        addBtn .backgroundColor=[UIColor orangeColor];
        [addBtn setTitle:@"+" forState:UIControlStateNormal];
        [addBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [addBtn addTarget:self action:@selector(addBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [v1 addSubview:addBtn];
        
        
        
        
        UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(addBtn.frame)+5, 0, 20, btn_h)];
        lab.text=@"1";
        lab.textAlignment=NSTextAlignmentRight;
        lab.font=[UIFont systemFontOfSize:14];
        lab.textColor=[UIColor blackColor];
        lab.tag=i+1000;
        [v1 addSubview:lab];
        v1.hidden=YES;
        UILabel *zhang=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(lab.frame), 0, 14, btn_h)];
        zhang.text=@"张";
        zhang.font=[UIFont systemFontOfSize:14];
        zhang.textColor=[UIColor blackColor];
        [v1 addSubview:zhang];
        
        UIButton *sureBtn1=[UIButton buttonWithType:UIButtonTypeCustom];
        sureBtn1.frame=CGRectMake(CGRectGetMaxX(zhang.frame)+5, 0, v1.frame.size.width-(CGRectGetMaxX(zhang.frame)+5)-5, btn_h);
        sureBtn1.backgroundColor=[UIColor redColor];
        [sureBtn1 setTitle:@"确定" forState:UIControlStateNormal];
        [sureBtn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        sureBtn1.titleLabel.font=[UIFont systemFontOfSize:12];
        sureBtn1.tag=i+4000;
        [sureBtn1 addTarget:self action:@selector(sureBtn1Click:) forControlEvents:UIControlEventTouchUpInside];
        [v1 addSubview:sureBtn1];
        
        
        
        
        UIView *v2=[[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(btn3.frame)+10, btn3.frame.origin.y, self.view.frame.size.width-(CGRectGetMaxX(btn3.frame)+10), btn_h)];
        v2.tag=i*10+200;
        [_scrollView addSubview:v2];
        UITextField *p1=[[UITextField alloc]initWithFrame:CGRectMake(10, 0, v2.frame.size.width-10-10-30-3, btn_h)];
        p1.tag=i+2000;
        p1.placeholder=@" 请输入账号";
        p1.font=[UIFont systemFontOfSize:14];
        p1.layer.borderColor=[UIColor orangeColor].CGColor;
        p1.layer.borderWidth=1.0;
        p1.layer.cornerRadius=2.0;
        [v2 addSubview:p1];
        v2.hidden=YES;
        UIButton *sureBtn2=[UIButton buttonWithType:UIButtonTypeCustom];
        sureBtn2.frame=CGRectMake(CGRectGetMaxX(p1.frame)+3, 0, 30, btn_h);
        sureBtn2.backgroundColor=[UIColor redColor];
        [sureBtn2 setTitle:@"确定" forState:UIControlStateNormal];
        [sureBtn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        sureBtn2.titleLabel.font=[UIFont systemFontOfSize:12];
        sureBtn2.tag=i+4000;
        [sureBtn2 addTarget:self action:@selector(sureBtn2Click:) forControlEvents:UIControlEventTouchUpInside];
        [v2 addSubview:sureBtn2];
        
        
        
        
        
        
        UIView *v3=[[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(btn3.frame)+10, btn3.frame.origin.y, self.view.frame.size.width-(CGRectGetMaxX(btn3.frame)+10), btn_h)];
        v3.tag=i*10+500;
        [_scrollView addSubview:v3];
        UITextField *p2=[[UITextField alloc]initWithFrame:CGRectMake(10, 0, v3.frame.size.width-10-10-30-3, btn_h)];
        p2.tag=i+3000;
        p2.placeholder=@" 请输入账号";
        p2.font=[UIFont systemFontOfSize:14];
        p2.layer.borderColor=[UIColor orangeColor].CGColor;
        p2.layer.borderWidth=1.0;
        p2.layer.cornerRadius=2.0;
        [v3 addSubview:p2];
        v3.hidden=YES;
        UIButton *sureBtn3=[UIButton buttonWithType:UIButtonTypeCustom];
        sureBtn3.frame=CGRectMake(CGRectGetMaxX(p1.frame)+3, 0, 30, btn_h);
        sureBtn3.backgroundColor=[UIColor redColor];
        [sureBtn3 setTitle:@"确定" forState:UIControlStateNormal];
        [sureBtn3 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        sureBtn3.titleLabel.font=[UIFont systemFontOfSize:12];
        sureBtn3.tag=i+5000;
        [sureBtn3 addTarget:self action:@selector(sureBtn3Click:) forControlEvents:UIControlEventTouchUpInside];
        [v3 addSubview:sureBtn3];
    }
    _scrollView.contentSize=CGSizeMake(self.view.frame.size.width, 20+44+20+(35+30+3+10)*arr.count);
}
//兑换一元优惠券
-(void)btn1Click:(UIButton *)sender
{
    NSInteger hang=(sender.tag-1)/10;
    UIView *v1=[self.view viewWithTag:hang*10+100];
    v1.hidden=NO;
    UIView *v2=[self.view viewWithTag:hang*10+200];
    v2.hidden=YES;
    UIView *v3=[self.view viewWithTag:hang*10+500];
    v3.hidden=YES;
}
-(void)btn2Click:(UIButton *)sender
{
    NSInteger hang=(sender.tag-2)/10;
    UIView *v1=[self.view viewWithTag:hang*10+100];
    v1.hidden=YES;
    UIView *v2=[self.view viewWithTag:hang*10+200];
    v2.hidden=NO;
    UIView *v3=[self.view viewWithTag:hang*10+500];
    v3.hidden=YES;
}
-(void)btn3Click:(UIButton *)sender
{
    NSInteger hang=(sender.tag-3)/10;
    UIView *v1=[self.view viewWithTag:hang*10+100];
    v1.hidden=YES;
    UIView *v2=[self.view viewWithTag:hang*10+200];
    v2.hidden=YES;
    UIView *v3=[self.view viewWithTag:hang*10+500];
    v3.hidden=NO;
}
-(void)addBtnClick:(UIButton *)sender
{
    UILabel *lab=[self.view viewWithTag:(sender.tag-400+1000)];
    NSLog(@"========%@",lab.text);
    NSInteger number=[lab.text integerValue];
    if (number<=19) {
        number+=1;
        lab.text=[NSString stringWithFormat:@"%ld",number];
    }
    
}
-(void)deleteBtnClick:(UIButton *)sender
{
    UILabel *lab=[self.view viewWithTag:(sender.tag-300+1000)];
     NSLog(@"========%@",lab.text);
    NSInteger number=[lab.text integerValue];
    if (number>=2) {
        number-=1;
        lab.text=[NSString stringWithFormat:@"%ld",number];
    }
}
-(void)sureBtn1Click:(UIButton *)sender
{
    
}
-(void)sureBtn2Click:(UIButton *)sender
{
    UITextField *text=[self.view viewWithTag:sender.tag-4000+2000];
    if (text.text.length<=0) {
        [self.view showTiShiMessage:@"账号不能为空"];
    }else{
         NSLog(@"********%@",text.text);
    }
   
}
-(void)sureBtn3Click:(UIButton *)sender
{
    UITextField *text=[self.view viewWithTag:sender.tag-5000+3000];
    if (text.text.length<=0) {
        [self.view showTiShiMessage:@"账号不能为空"];
    }else{
        NSLog(@"********%@",text.text);
    }
}
////兑换二元优惠券
//-(void)btn2Click:(UIButton *)sender
//{
//    NSInteger number=[_text2.text integerValue];
//    if ([_myTitle.text integerValue]>=180) {
//        if (number/180>0&&number<=_myNumber) {
//            _number2+=number/180;
//            _myTitle.text=[NSString stringWithFormat:@"%ld分",_myNumber-number/100*100];
//            _myNumber=[_myTitle.text integerValue];
//            _numberLab2.text=[NSString stringWithFormat:@"1元优惠券x%ld张",_number2];
//            _numberLab2.frame=CGRectMake(_numberLab2.frame.origin.x, _numberLab2.frame.origin.y, [self calculateWidthWithLable:_numberLab2], _numberLab2.frame.size.height);
//            _numberLab21.frame=CGRectMake(CGRectGetMaxX(_numberLab2.frame), _numberLab21.frame.origin.y, _numberLab21.frame.size.width, _numberLab21.frame.size.height);
//            self.changeMyScore(_myNumber-number/100*100);
//        }else if (number<0){
//            [self showAlertMessage:@"积分不能为负数"];
//        }else if (number>_myNumber){
//            [self showAlertMessage:@"积分不足"];
//        }
//        else{
//            if([_text2.text isEqualToString:@"0"]){
//                [self showAlertMessage:@"积分不能为0"];
//            }else{
//                [self showAlertMessage:@"积分必须大于或等于180"];
//            }
//        }
//
//    }else{
//        [self showAlertMessage:@"积分必须大于或等于180"];
//    }
//    
//}

//
//-(void)btn3Click:(UIButton *)sender
//{
//    
//}
//
//-(void)btn4Click:(UIButton *)sender
//{
//    
//}
-(void)showAlertMessage:(NSString *)message
{
    UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
    [alertView show];
}
//根据文字计算宽度
-(CGFloat)calculateWidthWithLable:(UILabel *)label
{
    CGSize size=[label.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:label.font,NSFontAttributeName, nil]];
    return size.width;
}

//获取所有优惠券
-(void)getAllQuanData
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    
//    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
//    NSString *token=[user objectForKey:@"token"];
        NSString *token=@"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEsImlzcyI6Imh0dHA6XC9cLzE5Mi4xNjguMTAuMTBcL2FwaVwvdXNlclwvYXV0aGVuIiwiaWF0IjoxNDYyOTQ0ODQ4LCJleHAiOjE0NjU1MzY4NDgsIm5iZiI6MTQ2Mjk0NDg0OCwianRpIjoiZTQ3YTUyYWJiODI5MjE5YjU1NGM3N2I5YzllMzBjZTMifQ.R_EW8BbONKYtsJI5h-Kpv0ctR4r1DTOrzZ-_FZnbriY";
    [manager GET:[NSString stringWithFormat:AllYouHuiQuan,token] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSArray *arr=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"所有的优惠券%@",arr);
        for (NSDictionary *dic in arr) {
            NSString *str=[NSString stringWithFormat:@"%@",dic[@"coupon_info"][@"discount"]];
            // NSLog(@"=======%f",discount);
            if ([str isEqualToString:@"1.00"]) {
                NSInteger value=[dic[@"coupon_info"][@"value"]integerValue];
                if (value==60) {
                    _num1+=1;
                }
            }
            if ([str isEqualToString:@"1.00"]) {
                NSInteger value=[dic[@"coupon_info"][@"value"]integerValue];
                if (value==150) {
                    _num2+=1;
                }
            }
            if ([str isEqualToString:@"0.80"]) {
                _num3+=1;
            }
            if ([str isEqualToString:@"0.50"]) {
                _num4+=1;
            }
            if ([str isEqualToString:@"0.00"]) {
                _num5+=1;
            }
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self initData];
            [_myTableView reloadData];
        });
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error%@",error.localizedDescription);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
